﻿// Author:  Marco van Wieren <mailto:mvwieren@gmail.com>
// Year:    2012
// License: Microsoft Public License (Ms-PL)

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web;

namespace Mvw.SharePoint.Taxonomy.AutoTagging
{
    public partial class WorkspacesSettings : LayoutsPageBase
    {
        public string PagePermissionFlagString;
        protected CheckBox WSAlwaysUpdateListElements;
        protected CheckBox WSDeleteWorkspaces;
        protected Button RunButton;
        
        protected override void OnInit(EventArgs e)
        {
            SPBasePermissions pagePermissionFlag = (SPBasePermissions)Enum.Parse(typeof(SPBasePermissions), PagePermissionFlagString.Replace("SPBasePermissions.", ""));
            // You can change the current setting by changing the following line in PopulateWorkspacesList.aspx: <%@Page PagePermissionFlagString="SPBasePermissions.ManageWeb"%>
            SPWeb web = SPContext.Current.Web;
            if (!web.DoesUserHavePermissions(pagePermissionFlag))
            {
                // build the access denied page
                SPUtility.Redirect(SPUtility.AccessDeniedPage + "?Source=" + SPHttpUtility.UrlKeyValueEncode(web.Site.MakeFullUrl(Request.RawUrl)),
                                   SPRedirectFlags.RelativeToLayoutsPage,
                                   HttpContext.Current);
            }
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                SPContext.Current.Web.AllowUnsafeUpdates = true;

                if (SPContext.Current.Web.AllProperties["WorkspacesSettings"] != null)
                {
                    var dict = SPContext.Current.Web.AllProperties["WorkspacesSettings"].ToString().GetDictionary();
                    try
                    {
                        if (dict.ContainsKey("WSAlwaysUpdateListElements"))
                        {
                            WSAlwaysUpdateListElements.Checked = bool.Parse(dict["WSAlwaysUpdateListElements"]);
                        }
                        if (dict.ContainsKey("WSDeleteWorkspaces"))
                        {
                            WSDeleteWorkspaces.Checked = bool.Parse(dict["WSDeleteWorkspaces"]);
                        }
                    }
                    catch { }
                }

                SPContext.Current.Web.AllowUnsafeUpdates = false;
            }
        }

        protected void RunButtonClick(object sender, EventArgs e)
        {
            try
            {
                SPContext.Current.Web.AllowUnsafeUpdates = true;
                HandleRunButtonClick();
            }
            catch (Exception ex)
            {
                this.ShowMessage(SPUtility.GetLocalizedString("$Resources:GLOBAL_ErrorMsgType", "SPM.Resources",
                    SPContext.Current.Web.Language), ex.Message, "red");
            }
            finally
            {
                SPContext.Current.Web.AllowUnsafeUpdates = false;
            }
        }

        private void HandleRunButtonClick()
        {
            using (var web = SPContext.Current.Site.OpenWeb(SPContext.Current.Web.ID))
            {
                web.AllowUnsafeUpdates = true;

                if (web.AllProperties["WorkspacesSettings"] != null)
                {
                    var dict = web.AllProperties["WorkspacesSettings"].ToString().GetDictionary();
                    if (dict.ContainsKey("WSAlwaysUpdateListElements"))
                    {
                        dict["WSAlwaysUpdateListElements"] = WSAlwaysUpdateListElements.Checked.ToString();
                    }
                    else
                    {
                        dict.Add("WSAlwaysUpdateListElements", WSAlwaysUpdateListElements.Checked.ToString());
                    }

                    if (dict.ContainsKey("WSDeleteWorkspaces"))
                    {
                        dict["WSDeleteWorkspaces"] = WSDeleteWorkspaces.Checked.ToString();
                    }
                    else
                    {
                        dict.Add("WSDeleteWorkspaces", WSDeleteWorkspaces.Checked.ToString());
                    }

                    web.AllProperties["WorkspacesSettings"] = dict.DictionaryToString();
                    web.Update();

                    // And report back to the user on what we did
                    this.ShowMessage(SPUtility.GetLocalizedString("$Resources:GLOBAL_InfoMsgType", "SPM.Resources", SPContext.Current.Web.Language),
                        SPUtility.GetLocalizedString("$Resources:WSS_WorkspacesSettingsUpdated", "SPM.Resources", SPContext.Current.Web.Language),
                        "green");
                }

                web.AllowUnsafeUpdates = false;
            }
        }
    }
}
